<?PHP

/**
*	Display Model
*
*	This model is used for the queries needed to display any information on publicly viewable pages.
*	
*/
class Display extends Model {
	
	/**
	*	Constructor function
	*
	*	Don't really do anything with this since the database is autoloaded from the config files.
	*
	*/
    function Display()
    {
        // Call the Model constructor
        parent::Model();
    }
	
	/**
	*	Function: full_series
	*
	*	Retrieves all reviews for a given seires, to be displayed on the /series/ view
	*	
	*	@param integer $id
	*
	*	@return array
	*/
	function full_series($id)
	{
		$query = $this->db->query("SELECT * FROM posts WHERE series='$id' ORDER BY p_id ASC");
        return $query->result();
	}
    
	/**
	*	Function: get_recent
	*
	*	Retrieves a recent subset of articles, used on the homepage. Used dynamic limit so it can be
	*	changed more easily during busy periods.
	*	
	*	@param integer $limit
	*
	*	@return array
	*/
    function get_recent($limit)
    {
        $query = $this->db->query("SELECT * FROM posts ORDER BY posted DESC LIMIT $limit");
        return $query->result();
    }
	
	/**
	*	Function: get_article
	*
	*	Retrieves a single article.
	*	
	*	@param integer $id
	*
	*	@return array
	*/
	function get_article($id)
	{
		$query = $this->db->query("SELECT * FROM posts WHERE p_id='$id'");
        return $query->row();
	}
	
	/**
	*	Function: get_article
	*
	*	Retrieves information for a single series.
	*	
	*	@param integer $id
	*
	*	@return array
	*/
	function get_series($id)
	{
		$query = $this->db->query("SELECT * FROM series WHERE s_id='$id'");
        return $query->row();
	}
	
	/**
	*	Function: list_reviewed
	*
	*	This function populates the /reviews/ view with all series that have had at least one review written about them.
	*	$cat is used to partially determine the sort of the results
	*	$id is used as an offset to move through the results
	*	$s is used as the other item to determine the sort of the results
	*	
	*	@param string $cat
	*	@param integer $id
	*	@param character $s
	*
	*	@return array
	*/
	function list_reviewed($cat,$id,$s)
	{
		
		if (strcmp($cat, 'default') == 0) {
	
			$start = $id;
			
			if ($s == 'A') {
				$sort = 'name ASC';
			} else {
				$sort = 'name DESC';
			}
		
		} else if (strcmp($cat, 'date') == 0) {
	
			$start = $id;
			
			if ($s == 'A') {
				$sort = 'year ASC, season_num ASC, name ASC';
			} else {
				$sort = 'year DESC, season_num DESC, name ASC';
			}
		
		}
		
		$query = $this->db->query("SELECT posts.series, series.name, series.season_num, series.season, series.year FROM posts INNER JOIN series ON posts.series=series.s_id GROUP BY series ORDER BY $sort LIMIT $start, 25");
		return $query->result();
	}

}
?>